package com.example.service.impl;

import com.example.dao.TrainingDao;
import com.example.domain.Department;
import com.example.domain.ExcelData;
import com.example.domain.Training;
import com.example.service.TrainingService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luo
 */
@Service
public class TrainingServiceImpl implements TrainingService {

    @Autowired
    private TrainingDao trainingDao;
    @Override
    public List<Training> selectTrain() {
        return trainingDao.selectTrain();
    }

    @Override
    public Training selectTrainById(Integer id) {
        return trainingDao.selectTrainById(id);
    }

    @Override
    public List<Training> selectTrainByName(Training training) {
        return trainingDao.selectTrainByName(training);
    }


    @Override
    public int addTrain(Training training) {
        return trainingDao.addTrain(training);
    }

    @Override
    public int updateTrain(Training training) {
        return trainingDao.updateTrain(training);
    }

    @Override
    public int deleteTrain(Integer id) {
        return trainingDao.deleteTrain(id);
    }

    @Override
    public PageInfo<Training> findPage(int page, int size) {
        // 开启分页插件,放在查询语句上面 帮助生成分页语句
        PageHelper.startPage(page, size); //底层实现原理采用改写语句 将下面的方法中的sql语句获取到然后做个拼接 limit AOP技术
        List<Training> listUser = trainingDao.selectTrain();
        // 封装分页之后的数据  返回给客户端展示  PageInfo做了一些封装 作为一个类
        PageInfo<Training> pageInfoUser = new PageInfo<Training>(listUser);
        return pageInfoUser;
    }
    /**
     * 结果的导出
     */
    @Override
    public ExcelData getOutputExcelProjectData() {


        //Excel实体类
        ExcelData excelData = new ExcelData();

        //找到表中需要添加的数据
        List<Training> list = trainingDao.selectTrain();

        //设置sheet名称
        excelData.setName("培训信息表");

        //设置表头字段
        List<String> titles = new ArrayList<String>();
        titles.add("Id");
        titles.add("员工姓名");
        titles.add("开始时间");
        titles.add("结束时间");
        titles.add("培训地点");
        titles.add("培训内容");
        titles.add("培训人数");
        excelData.setTitles(titles);

        //把expertUserList转换为List<List<Object>> rows
        List<List<Object>> rows = new ArrayList<List<Object>>();

        //加入数据
        for (Training training : list) {
            List<Object> row = new ArrayList<Object>();
            row.add(training.getId());
            row.add(training.getName());
            row.add(training.getStarttime());
            row.add(training.getEndtime());
            row.add(training.getSite());
            row.add(training.getContent());
            row.add(training.getAmount());
            rows.add(row);
        }

        //设置要输出的记录
        excelData.setRows(rows);
        return excelData;
    }

}
